<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="140px">
      <el-form-item label="计划推荐名称" prop="scheduleName">
        <el-input
          v-model="queryParams.scheduleName"
          placeholder="请输入计划推荐名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划健身器材" prop="scheduleFitnessEquipment">
        <el-select v-model="queryParams.scheduleFitnessEquipment" placeholder="请选择计划健身器材" clearable>
          <el-option
            v-for="dict in dict.type.sys_equipment"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="计划健身部位" prop="scheduleFitnessPart">
        <el-select v-model="queryParams.scheduleFitnessPart" placeholder="请选择计划健身部位" clearable>
          <el-option
            v-for="dict in dict.type.sys_body_part"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="计划难度" prop="scheduleFitnessDifficulty">
        <el-select v-model="queryParams.scheduleFitnessDifficulty" placeholder="请选择计划难度" clearable>
          <el-option
            v-for="dict in dict.type.sys_difficulty"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item style="float: right;">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:schedule:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:schedule:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:schedule:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:schedule:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="scheduleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column align="center" type="index" />
      <el-table-column label="计划推荐名称" align="center" prop="scheduleName" width="120px"/>
      <el-table-column label="计划健身器材" align="center" prop="scheduleFitnessEquipment" width="120px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_equipment" :value="scope.row.scheduleFitnessEquipment"/>
        </template>
      </el-table-column>
      <el-table-column label="计划健身部位" align="center" prop="scheduleFitnessPart" width="130px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_body_part" :value="scope.row.scheduleFitnessPart"/>
        </template>
      </el-table-column>
      <el-table-column label="计划健身时长(min)" align="center" prop="scheduleFitnessTime" width="150px"/>
      <el-table-column label="计划难度" align="center" prop="scheduleFitnessDifficulty" width="120px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_difficulty" :value="scope.row.scheduleFitnessDifficulty"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="计划内容"  align="center" prop="scheduleFitnessContent" /> -->
      <el-table-column label="计划内容" align="center" prop="scheduleFitnessContent">
        <template slot-scope="scope">
          <div class="contentClass">{{ delTag(scope.row.scheduleFitnessContent) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="handleShowDetail(scope.row)"
            v-hasPermi="['system:schedule:view']"
          >查看详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:schedule:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:schedule:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleApplication(scope.row)"
            v-hasPermi="['system:schedule:application']"
          >添加计划</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改健身计划推荐对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="计划推荐名称" prop="scheduleName">
              <el-input v-model="form.scheduleName" placeholder="请输入计划推荐名称"  :disabled="ShowDetail" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划健身器材" prop="scheduleFitnessEquipment">
              <el-select v-model="form.scheduleFitnessEquipment" placeholder="请选择计划健身器材" style="width: 100%;" :disabled="ShowDetail" >
                <el-option
                  v-for="dict in dict.type.sys_equipment"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="计划健身部位" prop="scheduleFitnessPart">
              <el-select v-model="form.scheduleFitnessPart" placeholder="请选择计划健身部位" style="width: 100%;" :disabled="ShowDetail" >
                <el-option
                  v-for="dict in dict.type.sys_body_part"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划健身时长(min)" prop="scheduleFitnessTime">
              <el-input v-model="form.scheduleFitnessTime" placeholder="请输入计划健身时长(min)" :disabled="ShowDetail"  />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="计划难度" prop="scheduleFitnessDifficulty">
              <el-select v-model="form.scheduleFitnessDifficulty" placeholder="请选择计划难度" style="width: 100%;" :disabled="ShowDetail" >
                <el-option
                  v-for="dict in dict.type.sys_difficulty"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="计划内容">
          <editor v-model="form.scheduleFitnessContent" :min-height="192" :disabled="ShowDetail" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSchedule, getSchedule, delSchedule, addSchedule, updateSchedule } from "@/api/system/schedule";
import { addFitnessplans } from "@/api/system/fitnessplans"

export default {
  name: "Schedule",
  dicts: ['sys_difficulty', 'sys_body_part', 'sys_equipment'],
  data() {
    return {
      // 遮罩层
      loading: true,
      ShowDetail:false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 健身计划推荐表格数据
      scheduleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        scheduleName: null,
        scheduleFitnessEquipment: null,
        scheduleFitnessPart: null,
        scheduleFitnessDifficulty: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        scheduleName: [
          { required: true, message: "计划推荐名称不能为空", trigger: "blur" }
        ],
        scheduleFitnessEquipment: [
          { required: true, message: "计划健身器材不能为空", trigger: "change" }
        ],
        scheduleFitnessPart: [
          { required: true, message: "计划健身部位不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询健身计划推荐列表 */
    getList() {
      this.loading = true;
      listSchedule(this.queryParams).then(response => {
        this.scheduleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //移除HTML标签
    delTag(str){
      return str.replace(/<[^>]+>/g,"");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        scheduleId: null,
        scheduleName: null,
        scheduleFitnessEquipment: null,
        scheduleFitnessPart: null,
        scheduleFitnessTime: null,
        scheduleFitnessDifficulty: null,
        scheduleFitnessContent: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.scheduleId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加健身计划推荐";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const scheduleId = row.scheduleId || this.ids
      getSchedule(scheduleId).then(response => {
        this.form = response.data;
        this.open = true;
        this.ShowDetail = false;
        this.title = "修改健身计划推荐";
      });
    },
    handleShowDetail(row){
      this.reset();
      const scheduleId = row.scheduleId || this.ids
      getSchedule(scheduleId).then(response => {
        this.form = response.data;
        this.open = true;
        this.ShowDetail = true;
        this.title = "健身计划推荐详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.scheduleId != null) {
            updateSchedule(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSchedule(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const scheduleIds = row.scheduleId || this.ids;
      this.$modal.confirm('是否确认删除健身计划推荐编号为"' + scheduleIds + '"的数据项？').then(function() {
        return delSchedule(scheduleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleApplication(row){
      const scheduleIds = row.scheduleId || this.ids;
      const scheduleName = row.scheduleName || this.ids;
      var now = new Date();
      const params ={
        userId:this.$store.state.user.id,
        scheduleId: row.scheduleId,
        fitnessPlanStarttime:now,
        fitnessPlanState:"state2",
      }
      this.$modal.confirm('是否确认添加健身计划推荐名称为"' + scheduleName + '"的数据项？').then(function() {
        return addFitnessplans(params).then(res=>{
                  console.log('res',res)
                  console.log("row",row)
                  console.log(params)
                });
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("添加成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/schedule/export', {
        ...this.queryParams
      }, `schedule_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
